package com.fly.lambda.framework.web.session.support;

import com.fly.lambda.framework.web.session.AccountSession;
import com.fly.lambda.framework.web.session.SessionManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Fly on 2016/7/19.
 */
public class SessionArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    SessionManage sessionManage;

    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterAnnotation(SessionInfo.class) != null;
    }

    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        AccountSession accountSession = sessionManage.getAccountSession(request);
        return accountSession;
    }
}
